﻿package com.zmcn.msg.business.msgGroup.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.zmcn.msg.bean.vo.MsgGroupVO;
import com.zmcn.msg.bean.vo.MsgVO;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.business.msgGroup.service.MsgGroupService;
import com.zmcn.msg.business.user.service.UserService;
import com.zmcn.msg.fw.action.BaseAction;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.util.CodeUtil;
import com.zmcn.msg.fw.util.OutUtil;
import com.zmcn.msg.pub.Const;

/**
 * 
 * 上传消息批次信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/msgGroup")
@Results({ @Result(name = "viewList", location = Const.JSP_msgGroup_list),@Result(name = "viewMsgList", location = Const.JSP_msg_Group_list),
		@Result(name = "viewImp", location = Const.JSP_msgGroup_imp),@Result(name = "viewEdit", location = Const.JSP_msgGroup_edit), 
		@Result(name = "viewFrontMsgGroup", location = Const.JSP_front_msgGroup) })
public class MsgGroupAction extends BaseAction {

	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(MsgGroupAction.class);
	private MsgGroupService service;
	// 上传文件域对象
	private File file;
	// 上传文件名
	private String fileFileName;
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Autowired
	public void setService(MsgGroupService service) {
		this.service = service;
	}

	/**
	 * 进入上传消息批次信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}
	
	public String viewMsgList() {
		return "viewMsgList";
	}

	/**
	 * 进入前台消息组列表页面
	 * 
	 * @return
	 */
	public String viewFrontMsgGroup() {
		try {
			HttpServletRequest req = getRequest();
			Page<MsgGroupVO> page = new Page<MsgGroupVO>(req);
			page.setPage(1);
			page.setPageSize(10);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			page.getParas().put("receiver", new String[] { userVO.getUserId() });
			service.getList(page);
			req.setAttribute("data", page.getResult());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewFrontMsgGroup";
	}

	/**
	 * 获取上传消息批次信息列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<MsgGroupVO> page = new Page<MsgGroupVO>(req);
			service.getList(page);
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
	
	/**
	 * 获取msg列表
	 * @return
	 */
	public String getMsgList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String batchId = req.getParameter("batchId");
			Page<MsgVO> page = service.getMsgGroupVO(new Page<MsgVO>(req),batchId);
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
 
	/**
	 * 导入
	 * @return
	 */
	public String imp() {
		HttpServletRequest req = getRequest();
		String user = req.getParameter("user");
		UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			if (service.imp(fileFileName, file,user,userVO)) {
				map.put("code", CodeUtil.SUCCESS);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
	
	/**
	 * 进入导入界面
	 * 
	 * @return
	 */
	public String viewImp() {
		List<UserVO> list= userService.getAllUserVO();
		HttpServletRequest req = getRequest();
		req.setAttribute("item", list);
		return "viewImp";
	}

}
